﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fa.Kanbox.API.Controls;
using Fa.Kanbox.API;

namespace Fa.Kanbox.API.Demo
{
    public partial class FLogin : Form
    {
        public FLogin()
        {
            InitializeComponent();
        }
        private void FMain_Shown(object sender, EventArgs e)
        {
            MyGlobal.LoadTokenInfos();
            comboBox1.DataSource = MyGlobal.TokenInfos;
            if (comboBox1.Items.Count > 0)
                comboBox1.SelectedIndex = 0;
            webBrowserToken1.ClientID = System.Configuration.ConfigurationManager.AppSettings["client_id"];
            webBrowserToken1.ClientSecret = System.Configuration.ConfigurationManager.AppSettings["client_secret"];
            webBrowserToken1.ToGetCode();

        }

        private void webBrowserToken1_GetCodeCompleted(GetCodeCompletedEventArgs e)
        {
            if (e.Exception != null)
            {
                MessageBox.Show(e.Exception.ToString());
                return;
            }
            MyGlobal.ApiClient.GetToken(e.Code);
            MyGlobal.TokenInfos.Add(MyGlobal.ApiClient.TokenInfo);
            MyGlobal.WriteTokenInfos();
            MyGlobal.FMain.Show();
            MyGlobal.FLogin.Hide();

        }

        private void bunLogin_Click(object sender, EventArgs e)
        {
            var info = comboBox1.SelectedItem as KbTokenInfo;
            if (info == null)
            {
                MessageBox.Show("请选择一个Token");
                return;
            }
            MyGlobal.ApiClient.RefreshToken(info.RefreshToken);
            MyGlobal.FMain.Show();
            MyGlobal.FLogin.Hide();
        }
    }
}
